package com.ljw.spring.annotation.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @FileName LinuxCondition
 * @Description TODO
 * @Author ljw
 * @Date 2021/2/1 17:10
 * @Version 1.0
 */
public class LinuxCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //1. 获取bean工厂
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //2. 类加载器
        ClassLoader classLoader = context.getClassLoader();
        //3. 环境变量
        Environment environment = context.getEnvironment();

        //4. BeanDefinitionRegistry：可以对IOC容器中的注册bean进行增删改查操作
        BeanDefinitionRegistry registry = context.getRegistry();

        String property = environment.getProperty("os.name");
        if (property.contains("Linux")){
            return true;
        }
        return false;
    }
}
